package com.jye.rseven.user.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jye.rseven.domain.Promotion;
import com.jye.rseven.service.PromotionService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/promotion")
@Api(tags = "被推广控制器API")
public class PromotionController {

    @Reference
    PromotionService promotionService;


    /*
     * 通过id查找被推广数据
     * */
    @GetMapping("/findPromotionById")
    @ApiOperation(value = "查找被推广", notes = "根据被推广id查找被推广数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "被推广ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findPromotionById(int id) {
        JsonResult jsonResult = new JsonResult();
        Promotion p = promotionService.findPromotionById(id);
        if (!StringUtils.isEmpty(p)) {
            jsonResult.setData(p);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 查找被推广
     * */
    @GetMapping("/findAllPromotion")
    @ApiOperation(value = "查找所有被推广", notes = "查找所有被推广", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findAllPromotion(int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Promotion> list = promotionService.findAllPromotion(skip, limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 生成被推广数据
     * */
    @PostMapping("/addPromotion")
    @ApiOperation(value = "生成被推广数据", notes = "通过pId和uId生成被推广数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "promotion",
                    value = "被推广实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.Promotion"),
    })
    public JsonResult addPromotion(Promotion promotion) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(promotionService.addPromotion(promotion));
        return jsonResult;
    }

    /*
     * 修改被推广数据
     * */
    @PutMapping("/updatePromotion")
    @ApiOperation(value = "修改被推广数据", notes = "修改佣金记录,只能修改状态和分享码、", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "promotion",
                    value = "被推广实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.Promotion"),
    })
    public JsonResult updatePromotion(Promotion promotion) {
        JsonResult jsonResult = new JsonResult();
        System.out.println(promotion);
        int i = promotionService.updatePromotion(promotion);
        if (i == 1) {
            jsonResult.setMsg("修改成功!");
        } else {
            jsonResult.setMsg("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    /*
     * 通过分享码查询被推广
     * */
    @GetMapping("/findPromotionByShareCode")
    @ApiOperation(value = "查找佣金记录", notes = "通过分享码shareCode查找佣金记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "shareCode",
                    value = "分享码", required = true,
                    dataType = "String"),
    })
    public JsonResult findPromotionByShareCode(String shareCode) {
        JsonResult jsonResult = new JsonResult();
        Promotion promotion = promotionService.findPromotionByShareCode(shareCode);
        if(!StringUtils.isEmpty(promotion)){
            jsonResult.setData(promotion);
        }else {
            jsonResult.setMsg("未查找到该佣金记录!请确认分享码是否正确!");
        }
        return jsonResult;
    }

}
